package lambdasinaction.chap6.ex;

import lambdasinaction.chap6.Dish;
import lambdasinaction.chap6.Grouping;
import org.junit.Test;

import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.*;
import static lambdasinaction.chap6.Dish.menu;

/**
 * Created by gx on 2017/4/14.
 */
public class Mytest {

    @Test//汇总统计流
    public void test1() {
        IntSummaryStatistics summaryStatistics = menu.stream().collect(summarizingInt(Dish::getCalories));
        System.out.println(summaryStatistics);
    }

    @Test//平均数
    public void test2() {
        double summaryStatistics = menu.stream().collect(averagingDouble(Dish::getCalories));
        System.out.println(summaryStatistics);
    }

    @Test//和
    public void test3() {
        int summaryStatistics = menu.stream().collect(summingInt(Dish::getCalories));
        System.out.println(summaryStatistics);
    }

    @Test//最大值
    public void test4() {
//        int summaryStatistics = menu.stream().collect(counting(Dish::getCalories));
//        System.out.println(summaryStatistics);
    }

    @Test//拼接字符串
    public void test5() {
        System.out.println(menu.stream().map(Dish::getName).collect(joining()));
        System.out.println(menu.stream().map(Dish::getName).collect(joining(",")));
    }


    // 分组
    @Test
    public void test6() {
        Map<Dish.Type, List<Dish>> map = menu.stream().collect(groupingBy(Dish::getType));
        System.out.println(map);
    }

    @Test
    public void test7() {
        Map<Dish.Type, List<Dish>> map = menu.stream().collect(groupingBy(Dish::getType));
        System.out.println(map);
    }

    @Test
    public void test8() {
        int a = 10;
        Boolean v = IntStream.range(2, a).noneMatch(i -> a % i == 0);
        System.out.println(v);
    }

    @Test
    public void test9() {
        int a = 10;
        Boolean v = IntStream.rangeClosed(2, a).noneMatch(i -> a % i == 0);
        System.out.println(v);
    }




}
